<template>
  <div>
    <el-card style="width: 40%;">
      <el-form label-width="80px" style="padding-right: 30px" :rules="rules" ref="formRef" :model="user" class="demo-form-inline">

        <div style="text-align: center">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:9090/file/upload"
              :show-file-list="false"
              :headers="{token: user.token}"
              :on-success="handleAvatarSuccess"
          >
            <img v-if="user.avatar" :src="user.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>

        <el-form-item style="margin-top: 30px;" label="用户名">
          <el-input v-model="user.username" disabled></el-input>
        </el-form-item>

        <el-form-item prop="password" style="margin-top: 30px;" label="原始密码">
          <el-input show-password v-model="user.password" disabled></el-input>
        </el-form-item>

        <el-form-item prop="NewPassword" style="margin-top: 30px;" label="新密码">
          <el-input show-password v-model="user.NewPassword"></el-input>
        </el-form-item>

        <el-form-item prop="SurePassword" style="margin-top: 30px;" label="确认密码">
          <el-input show-password v-model="user.SurePassword"></el-input>
        </el-form-item>


        <div style="text-align: center;margin-right: 70px">
          <el-form-item>
            <el-button type="primary" @click="onSubmit">立即修改</el-button>
          </el-form-item>
        </div>

      </el-form>
    </el-card>
  </div>
</template>

<script>
import reuqest from "@/utils/request";

export default {
  name: "PassWordView",
  data() {
    return {
      user: JSON.parse(localStorage.getItem('user11') || '{}'),
      rules: {
        password: [
            {required: true, message: '请输入原始密码', trigger: 'blur'},
        ],
        NewPassword: [
            {required: true, message: '请输入新密码', trigger: 'blur'},
        ],
        SurePassword: [
            {required: true, message: '请输入确认密码', trigger: 'blur'},
            {
              validator: (rule, value, callback) => {
                if (value !== this.user.NewPassword) {
                  callback(new Error('两次输入密码不一致'));
                } else {
                  callback();
                }
              },
              trigger: 'blur'
            }
        ]
      }
    };
  },
  methods: {

    //确认是否确认提交修改
    onSubmit() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.$confirm('确定用户信息吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.user.password = this.user.NewPassword
            //触发更新
            reuqest.post('/user/update', this.user).then(res => {
              if (res.code === 200) {
                this.$message.success('修改成功,请重新登陆')
                  this.$router.push('/')
              } else {
                // 登录失败
                this.$message.error(res.message)
              }
            })
          }).catch(() => {
            this.$message({type: 'info', message: '已取消修改'});
            setTimeout(() => {
              this.$router.go(0);
            }, 1000);
          })
        }
      });
    },
    handleAvatarSuccess(res, file, fileList) {
      this.user.avatar = res.data
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border-radius: 50%;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>